package com.woniuxy.kaer.shop.controller;



import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.kaer.commons.jwtUtil.JwtUtil;
import com.woniuxy.kaer.commons.util.PageInfo;
import com.woniuxy.kaer.commons.util.Result;
import com.woniuxy.kaer.dto.CollectProductDTO;
import com.woniuxy.kaer.form.AddProductForm;
import com.woniuxy.kaer.form.AddShoppingForm;
import com.woniuxy.kaer.form.PageForm;
import com.woniuxy.kaer.form.SearchProductForm;
import com.woniuxy.kaer.param.AddProductParam;
import com.woniuxy.kaer.param.AddShoppingParam;
import com.woniuxy.kaer.param.PageParam;
import com.woniuxy.kaer.param.SearchProductParam;
import com.woniuxy.kaer.shop.model.Product;
import com.woniuxy.kaer.shop.service.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 *  商品
 * </p>
 *
 * @author 郭建东
 * @since 2025年02月17日
 */
@RestController
@RequestMapping("/product")
@Api(tags = "商品管理")
public class ProductController {

    @Resource
    private ProductService productService;

    @Resource
    private JwtUtil jwtUtil;


    /**
     * 新增商品（管理员）
     *
     */
    @PostMapping("/addProduct")
    @ApiOperation("新增商品")
    private Result addProduct(@RequestBody AddProductForm addProductForm) throws Exception{
        productService.addProduct(BeanUtil.toBean(addProductForm, AddProductParam.class));
        return Result.succeed("新增商品成功",null);
    }

    //通过商品id修改商品售卖状态（管理员）
    @PostMapping("/updateProductState")
    @ApiOperation("修改商品售卖状态")
    private Result updateProductState(Integer productId) throws Exception{
        productService.updateProductState(productId);
        return Result.succeed("修改商品售卖状态成功",null);
    }

    //通过商品id修改商品价格（管理员）
    @PostMapping("/updateProductPrice")
    @ApiOperation("修改商品价格")
    private Result updateProductPrice(Integer productId,BigDecimal price) throws Exception{
        System.out.println(productId+" "+price);
        productService.updateProductPrice(productId,price);
        return Result.succeed("修改商品价格成功",null);
    }

    //通过商品id修改商品库存（管理员）
    @PostMapping("/updateProductInventory")
    @ApiOperation("修改商品库存")
    private Result updateProductInventory(Integer productId,Long inventory) throws Exception{
        productService.updateProductInventory(productId,inventory);
        return Result.succeed("修改商品库存成功",null);
    }

    //添加商品到收藏夹（用户）
    @PostMapping("/Collect")
    @ApiOperation("收藏商品")
    private Result productCollect(Integer productId,@RequestHeader("token") String token )throws Exception{
        Integer userId = jwtUtil.getUserId(token);
        productService.productCollect(productId,userId);
        return Result.succeed("收藏成功",null);
    }

    //取消收藏
    @PostMapping("/cancelCollect")
    @ApiOperation("取消收藏")
    private Result cancelCollect(Integer productId,@RequestHeader("token") String token )throws Exception{
        Integer userId = jwtUtil.getUserId(token);
        productService.cancelCollect(productId,userId);
        return Result.succeed("取消收藏成功",null);
    }

    //查看收藏商品（用户）
    @PostMapping("/searchCollect")
    @ApiOperation("查看收藏商品")
    private Result searchCollect(@RequestHeader("token") String token, @RequestBody PageForm form)throws Exception{
        Integer userId = jwtUtil.getUserId(token);
        PageInfo productPageInfo = productService.searchCollect(userId, form);
        return Result.succeed("查询成功",productPageInfo);
    }

    //查看收藏排行
    @GetMapping("/selectCollectRank")
    @ApiOperation("查看收藏排行")
    public Result selectCollectRank()throws Exception{
        List<CollectProductDTO> products = productService.selectCollectRank();
        return Result.succeed("查询成功",products);
    }

    //通过关键字查询商品（用户）
    @PostMapping("/searchProduct")
    @ApiOperation("查询商品")
    private Result searchProductByKeyword(@RequestBody SearchProductForm form)throws Exception{
        System.out.println(form.getKeyword());
        PageInfo productPageInfo = productService.searchProductByKeyword(BeanUtil.toBean(form, SearchProductParam.class));
        return Result.succeed("查询成功",productPageInfo);
    }

    //添加商品到购物车
    @PostMapping("/addShopping")
    @ApiOperation("添加购物车")
    private Result addShoppingCar(@RequestBody AddShoppingForm form, @RequestHeader("token") String token)throws Exception{
        Integer userId = jwtUtil.getUserId(token);
        productService.addShoppingCar(BeanUtil.toBean(form, AddShoppingParam.class),userId);
        return Result.succeed("添加购物车成功",null);
    }

    //查看购物车
    @PostMapping("/searchMyShoppingCar")
    @ApiOperation("查看购物车")
    public Result searchMyShoppingCar(@RequestHeader("token") String token,@RequestBody PageForm form)throws Exception{
        Integer userId = jwtUtil.parseToken(token).get("userId", Integer.class);
        return Result.succeed("查询成功",productService.searchMyShoppingCar(BeanUtil.toBean(form, PageParam.class),userId));
    }

    @PostMapping("/upImage")
    @ApiOperation("上传图片")
    public Result upImage(MultipartFile file) throws Exception{
        return Result.succeed( "上传图片成功", productService.upImage(file));
    }


}

